/* 
 * @description: //单条面积图
 * @author: Jane 
 * @date: 2023-10-10 14:06:53
 */
import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';

class LineAreaChart extends StatelessWidget {
  const LineAreaChart({required this.isShowingMainData,required this.lineColor});

  final bool isShowingMainData;
  final Color lineColor;

  @override
  Widget build(BuildContext context) {
    return LineChart(
      isShowingMainData ? sampleData2 : sampleData2,
      duration: const Duration(milliseconds: 250),
    );
  }

  LineChartData get sampleData2 => LineChartData(
        lineTouchData: lineTouchData2,
        gridData: gridData,
        titlesData: titlesData2,
        borderData: borderData,
        lineBarsData: lineBarsData2,
        minX: 0,
        maxX: 14,
        maxY: 6,
        minY: 0,
      );

  LineTouchData get lineTouchData2 => const LineTouchData(
        enabled: false,
      );

  FlTitlesData get titlesData2 => FlTitlesData(
        bottomTitles: AxisTitles(
          sideTitles: bottomTitles,
        ),
        rightTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        topTitles: const AxisTitles(
          sideTitles: SideTitles(showTitles: false),
        ),
        leftTitles: AxisTitles(
          sideTitles: leftTitles(),
        ),
      );

  List<LineChartBarData> get lineBarsData2 => [
        lineChartBarData2_2,
      ];

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      fontWeight: FontWeight.bold,
      fontSize: 14,
    );
    String text;
    switch (value.toInt()) {
      case 1:
        text = '1m';
        break;
      case 2:
        text = '2m';
        break;
      case 3:
        text = '3m';
        break;
      case 4:
        text = '5m';
        break;
      case 5:
        text = '6m';
        break;
      default:
        return Container();
    }

    return Text(text, style: style, textAlign: TextAlign.center);
  }

  SideTitles leftTitles() => SideTitles(
        getTitlesWidget: leftTitleWidgets,
        showTitles: false,
        interval: 1,
        reservedSize: 100,
      );

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    TextStyle style =
        TextStyle(fontSize: ScreenHelper.sp(14), color: AppTheme.weakTextColor);
    Widget text;

    return SideTitleWidget(
      axisSide: meta.axisSide,
      space: 10,
      fitInside: const SideTitleFitInsideData(
          enabled: true,
          axisPosition: 1,
          parentAxisSize: 10,
          distanceFromEdge: 10),
      child: Text(
        "1/4",
        style: style,
      ),
    );
  }

  SideTitles get bottomTitles => SideTitles(
        showTitles: true,
        reservedSize: 32,
        interval: 3,
        getTitlesWidget: bottomTitleWidgets,
      );

  FlGridData get gridData => FlGridData(
        show: true,
        drawVerticalLine: false,
        horizontalInterval: 1,
        getDrawingHorizontalLine: (value) {
          return FlLine(
            color: AppTheme.dividerColor,
            strokeWidth: 1,
          );
        },
        checkToShowHorizontalLine: (double value) {
          return value == 1 || value == 6 || value == 4 || value == 5;
        },
      );

  FlBorderData get borderData => FlBorderData(
        show: true,
        border: Border(
          bottom: BorderSide(color: Colors.grey.withOpacity(0.2), width: 1),
          left: const BorderSide(color: Colors.transparent),
          right: const BorderSide(color: Colors.transparent),
          top: const BorderSide(color: Colors.transparent),
        ),
      );

  LineChartBarData get lineChartBarData2_2 => LineChartBarData(
        isCurved: true, //是否绘制成曲线
        color: lineColor.withOpacity(0.5),
        barWidth: 2,
        isStrokeCapRound: false,
        isStrokeJoinRound: true,
        dotData: FlDotData(
          show: true,
          getDotPainter: (spot, percent, barData, index) => FlDotCirclePainter(
              radius: 4,
              color: Colors.transparent,
              strokeColor: lineColor,
              strokeWidth: 1),
        ),

        belowBarData: BarAreaData(
            show: true,
            color: lineColor.withOpacity(0.2),
            gradient: LinearGradient(colors: [
              lineColor.withOpacity(.4),
              lineColor.withOpacity(.0)
            ], begin: Alignment.topCenter, end: Alignment.bottomCenter)),
        spots: const [
          FlSpot(1, 1),
          FlSpot(3, 2.8),
          FlSpot(7, 1.2),
          FlSpot(10, 2.8),
          FlSpot(12, 2.6),
          FlSpot(13, 3.9),
        ],
      );
}

